package com.innerchic.weqiuqiu.adapter;

import android.content.Intent;
import android.content.IntentSender;
import android.graphics.Bitmap;
import android.graphics.drawable.Icon;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.innerchic.annotations.base.BindViewLayoutHolder;
import com.innerchic.mapper.app.SuperAdapter;
import com.innerchic.mapper.log.Logger;
import com.innerchic.weqiuqiu.R;
import com.innerchic.weqiuqiu.app.RecyclerItemTouchHelper;
import com.innerchic.weqiuqiu.assembly.SendMessageDialog;
import com.innerchic.weqiuqiu.bean.MessageBean;
import com.innerchic.weqiuqiu.emoji.Emoji;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：wedudu
 * 日    期：2022/1/17 10:02
 * 包    名：com.innerchic.weqiuqiu.adapter
 * 描    述：消息列表适配器
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
@BindViewLayoutHolder("adapter_message_list")
public class MessageListAdapter extends SuperAdapter<MessageBean> implements RecyclerItemTouchHelper.ItemTouchHelperCallback {

    /**
     * 消息发送弹窗
     */
    private SendMessageDialog sendMessageDialog;

    @Override

    protected void adapterBindViewHolder(RecyclerView.ViewHolder holder, MessageBean data, int position) {
        MessageListAdapterHolder format = MessageListAdapterHolder.format(holder);
        //设置头像
        Parcelable parcelable = data.getParcelable();
        if (parcelable == null) {
            format.imageHead.setImageResource(R.mipmap.pic_user);
        } else {
            if (parcelable.getClass().equals(Icon.class)) {
                format.imageHead.setImageIcon((Icon) parcelable);
            } else if (parcelable.getClass().equals(Bitmap.class)) {
                format.imageHead.setImageBitmap((Bitmap) parcelable);
            } else {
                format.imageHead.setImageResource(R.mipmap.ic_launcher);
            }
        }
        //设置来源标识
        String messageSource = data.getMessageSource();
        format.redPackageBtn.setVisibility(View.GONE);
        if ("com.tencent.mm".equals(messageSource)) {
            format.sourceImage.setImageResource(R.mipmap.pic_wechat);
            if (data.getMessage().startsWith("[微信红包]")) {
                format.redPackageBtn.setVisibility(View.VISIBLE);
                format.redPackageBtn.setOnClickListener(v -> openIntent(data));
            }
        } else if ("com.tencent.mobileqq".equals(messageSource)) {
            format.sourceImage.setImageResource(R.mipmap.pic_mobileqq);
        } else if ("".equals(messageSource)) {
            format.sourceImage.setImageResource(R.mipmap.pic_dingtolk);
        } else {
            format.sourceImage.setImageResource(R.mipmap.pic_android);
        }
        //消息
        if ("com.tencent.mm".equals(messageSource)) {
            format.contentText.setText(Emoji.decode(getContext(), data.getMessage()));
            format.contentView.setOnClickListener(v -> {
                sendMessageDialog = new SendMessageDialog(getContext());
                sendMessageDialog.setMessageBean(data);
                sendMessageDialog.setSendOver((bean, sendText) -> {
                    boolean wechat_reply_close_paopao = getContext()
                            .getSharedPreferences("function_wechat_status", 0)
                            .getBoolean("wechat_reply_close_paopao", true);
                    if (wechat_reply_close_paopao) {
                        remove(position);
                    }
                });
                sendMessageDialog.show();
                sendMessageDialog.getContentFocus();
            });
        } else {
            format.contentText.setText(data.getMessage());
            format.contentView.setOnClickListener(v -> {
                Toast.makeText(getContext(), "当前消息不支持快捷回复", Toast.LENGTH_SHORT).show();
            });
        }

        //长按进入主页
        format.contentView.setOnLongClickListener(v -> {
            openIntent(data);
            return true;
        });

        if (data.getCount() == 0) {
            format.countView.setVisibility(View.GONE);
        } else {
            format.countView.setVisibility(View.VISIBLE);
            format.numberText.setText(String.valueOf(data.getCount()));
        }

        format.titleText.setText(data.getName());

        //设置定时
        if (data.getCloseTime() > 0) {
            getMainHandler().postDelayed(() -> {
                remove(data);
            }, data.getCloseTime());
        }
    }

    @Override
    protected boolean hasParent() {
        return true;
    }

    @Override
    protected ViewGroup parentLayout(ViewGroup parent) {
        LinearLayout linearLayout = new LinearLayout(parent.getContext());
        linearLayout.setGravity(Gravity.END);
        return linearLayout;
    }

    @Override
    protected int getLayoutRes(int viewType) {
        return R.layout.adapter_message_list;
    }

    @Override
    public void onItemRight(int position) {
        remove(position);
    }

    @Override
    public void onItemLeft(int position) {
        MessageBean bean = dataContent.get(position);
        String messageSource = bean.getMessageSource();
        String name = bean.getName();
        removeIf(messageBean -> {
            String a1 = messageBean.getMessageSource();
            String a2 = messageBean.getName();
            return a1.equals(messageSource) && a2.equals(name);
        });
    }

    /**
     * 启动项
     *
     * @param bean 对象
     */
    private void openIntent(MessageBean bean) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            getContext().startIntentSender(bean.getOpenPendingIntent().getIntentSender(), intent, Intent.FLAG_ACTIVITY_NEW_TASK,
                    Intent.FLAG_ACTIVITY_NEW_TASK, 0);
        } catch (IntentSender.SendIntentException | NullPointerException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "启动异常,当前消息无法打开..", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onItemTop(int position) {
    }

    @Override
    public void onItemBottom(int position) {
    }

    @Override
    public boolean onMove(int fromPosition, int toPosition) {
        Logger.d("Post:(" + fromPosition + "," + toPosition + ")");
        return false;
    }

}
